﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblChipDAO
    {
        private DBService _db = null;
        public TblChipDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }
        public bool InsUpdItem(TblChip chip, int mode)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblChip_InsUp",
                    new string[] { "@IDChip", "@ChipName", "@QuantityOnHand", "@ValueOnHand", "@ItemDescription", "@IDType", "@Price", "@Mode"},
                    new object[] { chip.IDChip, chip.ChipName, chip.QuantityOnHand, chip.ValueOnHand, chip.ItemDescription, chip.IDType, chip.Price, mode },
                    CommandType.StoredProcedure);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return flag;
        }


        public DataTable GetChipList()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select IDChip, ChipName, ItemDescription, TypeName, b.IDType, Price from TblChip a , TblType b where a.IDType = b.IDType", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


        public bool DeleteChip(int IDChip)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("delete TblChip where IDChip = @IDChip",
                    new string[] { "@IDChip" },
                    new object[] { IDChip },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

      

        public TblChip GetChipByID(int IDChip)
        {
            TblChip chip = new TblChip();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("select IDChip, ChipName, ItemDescription, TypeName, b.IDType, Price from TblChip a , TblType b where a.IDType = b.IDType and IDChip = @IDChip",
                      new string[] { "@IDChip" },
                    new object[] { IDChip }
                    , CommandType.Text);
                foreach (DataRow dr in dt.Rows)
                {
                    chip.IDChip = IDChip;
                    chip.ChipName = dr["ChipName"].ToString();
                    chip.Price = Decimal.Parse(dr["Price"].ToString());
                    chip.ItemDescription = dr["ItemDescription"].ToString();
                    chip.IDType = Convert.ToInt32(dr["IDType"].ToString()); 


                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return chip;
        }
    }
}
